<template>
  <div class="app-container">
    <el-form :model="pd.queryParams" ref="queryRef" :inline="true" v-show="pd.showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input v-model="pd.queryParams.name" placeholder="请输入名称" clearable
                  @keyup.enter="handleQuery"/>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['wms:orderType:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['wms:orderType:export']">导出
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="pd.showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="pd.loading" :data="pd.typeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="作业编码" align="center" prop="typeId"/>
      <el-table-column label="名称" align="center" prop="name"/>
      <el-table-column label="仓储类型" align="center" prop="stockType">
        <template #default="scope">
          <dict-tag :options="wms_order_type" :value="scope.row.stockType"/>
        </template>
      </el-table-column>
      <el-table-column label="财务类型" align="center" prop="fmsType">
        <template #default="scope">
          <dict-tag :options="fms_type" :value="scope.row.fmsType"/>
        </template>
      </el-table-column>
      <el-table-column label="是否启用" align="center" prop="enableFlag">
        <template #default="scope">
          <dict-tag :options="sys_is" :value="scope.row.enableFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="关联作业" align="center" prop="nextCreateType"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="创建者" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy"/>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['wms:orderType:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['wms:orderType:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="pd.total>0" :total="pd.total" v-model:page="pd.queryParams.pageNum"
                v-model:limit="pd.queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改产品入库录对话框 -->
    <el-dialog :title="pd.title" v-model="pd.open" width="500px" append-to-body>
      <el-form ref="typeRef" :model="pd.form" :rules="pd.rules" label-width="80px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="pd.form.name" placeholder="请输入名称"/>
        </el-form-item>

        <el-form-item label="仓储类别" prop="stockType">
          <el-select v-model="pd.form.stockType" placeholder="请选择">
            <el-option
                v-for="dict in wms_order_type"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="财务类别" prop="fmsType">
          <el-select v-model="pd.form.fmsType" placeholder="请选择">
            <el-option
                v-for="dict in fms_type"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="是否启用" prop="enableFlag">
          <el-select v-model="pd.form.enableFlag" placeholder="请选择是否启用">
            <el-option
                v-for="dict in sys_is"
                :key="dict.code"
                :label="dict.name"
                :value="dict.code"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="pd.form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="OrderType">
import {orderAddType, orderDelType, orderEditType, orderGetTypeList} from "@/api/wms/order";
import {getCurrentInstance, reactive} from "vue";

const {proxy} = getCurrentInstance();
const {
  sys_is,
  wms_order_type,
  fms_type
} = proxy.useDict('sys_is', 'wms_order_type', 'fms_type');

const pd = reactive({
  typeList: [],
  showSearch: true,
  open: false,
  loading: true,
  ids: [],
  single: true,
  multiple: true,
  total: 0,
  title: "",
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    type: null,
  },
  rules: {
    name: [
      {required: true, message: "名称不能为空", trigger: "blur"}
    ],
    type: [
      {required: true, message: "作业类型不能为空", trigger: "change"}
    ],
  }
});

/** 查询产品入库录列表 */
function getList() {
  pd.loading = true;
  orderGetTypeList(pd.queryParams).then(response => {
    pd.typeList = response.data;
    pd.total = response.total;
    pd.loading = false;
  });
}

// 取消按钮
function cancel() {
  pd.open = false;
  reset();
}

// 表单重置
function reset() {
  pd.form = {
    typeId: null,
    name: null,
    stockType: null,
    fmsType: null,
    enableFlag: null,
    nextCreateType: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  proxy.resetForm("typeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  pd.queryParams.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  pd.ids = selection.map(item => item.typeId);
  // pd.single = selection.length != 1;
  // pd.multiple = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  pd.open = true;
  pd.title = "添加产品入库录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
    pd.form = row;
    pd.open = true;
    pd.title = "修改出入库类别";
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["typeRef"].validate(valid => {
    if (valid) {
      if (pd.form.typeId != null) {
        orderEditType(pd.form).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          pd.open = false;
          getList();
        });
      } else {
        orderAddType(pd.form).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          pd.open = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || pd.ids;
  proxy.$modal.confirm('是否确认删除产品入库录编号为"' + _ids + '"的数据项？').then(function () {
    return orderDelType(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/type/export', {
    ...queryParams
  }, `type_${new Date().getTime()}.xlsx`)
}

getList();
</script>
